(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.resourceBundle.uploadInitialController', uploadInitialController);
	uploadInitialController.$inject = [ '$scope', 'Constants', 'modalService', 'gridService', 'layoutConfig', 'validatorConfig', 'authService',
	                                    'storageService', 'resourceBundleService' ];
	function uploadInitialController($scope, Constants, modalService, gridService, layoutConfig, validatorConfig, authService, storageService, resourceBundleService) {
		var ctrl = this, resourceBundleInfo = $scope.$stateParams.params, errorMessage = {};
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.resourceBundleUpload = resourceBundleInfo;
		
		ctrl.init = function() {
			authService.uploadInitial().then(function(response) {
				ctrl.resourceBundleUpload = response.data;
				ctrl.resourceBundleUpload.siteCode = storageService.getUserInfo().siteCode;
			});
			resourceBundleService.getResourceBundleScopeOptions().then(function(response) {
				ctrl.scopeOptions = response.data;
				if( !ctrl.resourceBundleUpload.scope){
					ctrl.resourceBundleUpload.scope = response.data[0].value;
				}
			});
		};

		ctrl.upload = function() {
			if (!ctrl.resourceBundleUpload.uploadFiles
					|| !ctrl.resourceBundleUpload.uploadFiles[0]
					|| !ctrl.resourceBundleUpload.uploadFiles[0].docFileName) {
				modalService
						.openErrorDialogs({
							'messages' : [ 'cda.resourcebundleupload.uploadfiles.required' ]
						});
				return;
			}
			var fileName = ctrl.resourceBundleUpload.uploadFiles[0].docFileName;
			ctrl.resourceBundleUpload.docId = ctrl.resourceBundleUpload.uploadFiles[0].docId;
			var position = fileName.lastIndexOf(".");
			var fmt = fileName.substring(position + 1, fileName.length);
			if (fmt.toLowerCase() != "xlsx") {
				modalService.openErrorDialogs({
					'messages' : [ 'resourcebundleupload.file.format.invalid' ]
				});
				return;
			} 
			return authService.upload(ctrl.resourceBundleUpload).then(
					uploadSuccessful, uploadFailed);
			
		};

		function uploadSuccessful(response) {
//			delete ctrl.piLoad.fileList;//clear file list
			modalService.openAcknowledgeDialogs({
				'messages' : [ 'messages.resourcebundle.upload.success' ],
				'btnCallback' : {
					'close' : function() {
						$scope.go('cda.fileLoad.list');
					}
				},
				'redirections' : [ {
					'name' : 'title.infobar.workflow.list',
					'state' : 'cda.workflow.wfSetup.list',
					'params' : {}
				}, {
					'name' : 'title.infobar.fileupload.list',
					'state' : 'cda.fileLoad.list',
					'params' : {}
				}]
			});
		}

		function uploadFailed(response) {
			if(!response.error || response.error == ''){
				response.error = 'resourcebundle.file.content.invalid';
			}
			modalService.openErrorDialogs({
				'messages' : response.error
			});
		}
		
		ctrl.setResBundleDesc = function() {
			if(ctrl.resourceBundleUpload.localeCode && ctrl.localOptions != null){
				angular.forEach(ctrl.localOptions, function(localOption, key){
					if(localOption.value == ctrl.resourceBundleUpload.localeCode){
						ctrl.resourceBundleUpload.resBundleDesc = localOption.label;
					}
				});
			}
		};
		
		ctrl.scopeChanged = function() {
			if(ctrl.resourceBundleUpload.scope){
				resourceBundleService.getlocaleCodeOptions(ctrl.resourceBundleUpload.scope).then(function(response) {
					if(response){
						ctrl.localOptions = response.data;
					}
				});
			}
		};
	}
})(angular);